package com.cat.yygh.sms.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.cat.yygh.sms.service.SmsService;
import com.cat.yygh.sms.utils.RandomUtil;
import com.cat.yygh.vo.msm.MsmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {
    /*注入redis*/
    @Autowired
    private RedisTemplate redisTemplate;
    /*发送短信服务*/
    public boolean sendSmsByPost(String path, String postContent) {
        URL url = null;
        try {
            url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            httpURLConnection.connect();
            OutputStream os=httpURLConnection.getOutputStream();
            os.write(postContent.getBytes("UTF-8"));
            os.flush();
            int httpRspCode = httpURLConnection.getResponseCode();
            if (httpRspCode == HttpURLConnection.HTTP_OK) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendCode(String phone) {
        redisTemplate.opsForValue().set(phone,"8888",50, TimeUnit.DAYS);
        return true;
//        //短信下发
//        String sendUrl = "https://smssh1.253.com/msg/v1/send/json";
//        String random = RandomUtil.getFourBitRandom();
//        System.out.println(random);
//        Map map = new HashMap();
//        map.put("account","N2207984");//API账号
//        map.put("password","2kDsIiy8m");//API密码
//        map.put("msg","code:"+random);//短信内容
//        map.put("phone",phone);//手机号
//        map.put("report","true");//是否需要状态报告
//        JSONObject js = (JSONObject) JSONObject.toJSON(map);
//        boolean status = sendSmsByPost(sendUrl, js.toString());
//        if (status){
//            redisTemplate.opsForValue().set(phone,random,50, TimeUnit.DAYS);
//            System.out.println(random);
//            return true;
//        }
//        return false;
    }
    //发送短信实现
    @Override
    public boolean send(MsmVo msmVo) {
        if(!StringUtils.isEmpty(msmVo.getPhone())) {
            //String code = (String)msmVo.getParam().get("code");
            //仅为了测试
            String code = RandomUtil.getFourBitRandom();
            System.out.println(msmVo);
            return true;
        }
        return false;
    }
}
